#pragma once
#include "/home/ys/linux_-demo/Project_Muduo/source/server.hpp"

class EchoServer
{
private:
    TCPServer tcpserver;

private:
    void OnConnected(const PtrConnection &conn)
    {
        DEBUG_LOG("NEW CONNECTION:%p", conn.get());
    }
    void OnClosed(const PtrConnection &conn)
    {
        DEBUG_LOG("CLOSE CONNECTION:%p", conn.get());
    }
    void OnMessage(const PtrConnection &conn, Buffer *buf)
    {
        conn->Send(buf->GetReadPos(), buf->ReadableSize());
        buf->MoveReadOffset(buf->ReadableSize());
        conn->Shutdown();
    }

public:
    EchoServer(int port)
        : tcpserver(port)
    {
        tcpserver.SetThreadCount(2);
        tcpserver.EnableInactiveRelease(10);
        tcpserver.SetCloseCallback(std::bind(&EchoServer::OnClosed, this, std::placeholders::_1));
        tcpserver.SetConnectedCallBack(std::bind(&EchoServer::OnConnected, this, std::placeholders::_1));
        tcpserver.SetMessageCallback(std::bind(&EchoServer::OnMessage, this, std::placeholders::_1, std::placeholders::_2));
    }
    void Start()
    {
        tcpserver.Start();
    }
};